using Android.Content;
using SQLite;
using CapstoneProject.Models;
using System.Collections.Generic;

namespace CapstoneProject.DataAccess
{
    public class OrmAccountRepository : IAccountRepository
    {
        private AccountDatabaseHelper helper;

        private OrmAccountRepository(Context context)
        {
            helper = new AccountDatabaseHelper(context);
        }

        public IList<Account> GetAllAccounts()
        {
            using (var database = new SQLiteConnection(helper.WritableDatabase.Path))
            {
                IList<Account> accountList = new List<Account>();
                foreach (Account a in database.Table<Account>())
                {
                    accountList.Add(a);
                }
                return accountList;
            }
        }

        public long AddAccount(string username, string password, string fiUrl, string fiOrg, string fid, string accountId, string bankId, string accountName, int userId)
        {
            using (var database = new SQLiteConnection(helper.WritableDatabase.Path))
            {
                return database.Insert(new Account
                {
                    Username = username,
                    Password = password,
                    FiUrl = fiUrl,
                    FiOrg = fiOrg,
                    Fid = fid,
                    BankAccountId = accountId,
                    BankId = bankId,
                    AccountName = accountName,
                    UserId = userId
                });
            }
        }

        public void UpdateAccount(int accountId, double balance)
        {
            using (var database = new SQLiteConnection(helper.WritableDatabase.Path))
            {
                Account updateAccount = database.Get<Account>(accountId);
                database.Update(updateAccount);
            }
        }

        public void DeleteAccount(int accountId)
        {
            using (var database = new SQLiteConnection(helper.WritableDatabase.Path))
            {
                Account deleteAccount = database.Get<Account>(accountId);
                database.Delete<Account>(deleteAccount);
            }
        }
    }
}